//
//  CGCompanyDetailOnceViewController.m
//  SchoolClient
//
//  Created by mac on 15/12/11.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGCompanyDetailOnceViewController.h"

@interface CGCompanyDetailOnceViewController ()

@property(nonatomic,assign)CGFloat fontSize;

@end

@implementation CGCompanyDetailOnceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"职位详情";
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.fontSize = 13;
    //发送请求获取公司产品详情
    [self fetchInfoWithProduct_id:self.company_id];
    [self layoutUI];
}

-(void)layoutUI{
    //容器View
    CGRect bFrame = CGRectMake(10, 10, SCREEN_WIDTH - 20, 270);
    UIView *containView = [[UIView alloc]initWithFrame:bFrame];
    containView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:containView];
    
    //招聘标题Label
    CGRect cFrame = CGRectMake(5, 0, SCREEN_WIDTH - 30, 44);
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:cFrame];
    titleLabel.text = self.myCompanyInfo.title;
    titleLabel.textColor = [UIColor blueColor];
    titleLabel.font = [UIFont systemFontOfSize:self.fontSize + 3];
    titleLabel.numberOfLines = 2;
    [containView addSubview:titleLabel];
    
    //公司名称Label
    cFrame.origin.y = CGRectGetMaxY(titleLabel.frame);
    cFrame.size.height = 20;
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:cFrame];
    nameLabel.text = self.myCompanyInfo.name;
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.font = [UIFont systemFontOfSize:self.fontSize + 1];
    [containView addSubview:nameLabel];
    
    //所属行业Label
    cFrame.origin.y = CGRectGetMaxY(nameLabel.frame);
    UILabel *industryLabel = [[UILabel alloc]initWithFrame:cFrame];
    //industryLabel.text = @"金融/银行  销售类";
    industryLabel.text = [NSString stringWithFormat:@"%@  %@",self.myCompanyInfo.industryType,self.myCompanyInfo.occupationType];
    industryLabel.textColor = [UIColor grayColor];
    industryLabel.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:industryLabel];
    
    //分隔线
    cFrame.origin.y += 28;
    cFrame.size.height = 1;
    UIView *lineView = [[UIView alloc]initWithFrame:cFrame];
    lineView.backgroundColor = [Tools getColor:@"ebebeb"];
    [containView addSubview:lineView];
    
    //薪资Label
    cFrame.origin.y += 12;
    cFrame.size.width = 50;
    cFrame.size.height = 20;
    UILabel *item1 = [[UILabel alloc]initWithFrame:cFrame];
    item1.text = @"薪资:";
    item1.textColor = [UIColor orangeColor];
    item1.font = [UIFont systemFontOfSize:self.fontSize + 3];
    [containView addSubview:item1];
    
    cFrame.origin.x += 50;
    cFrame.size.width = SCREEN_WIDTH - 30 - 50;
    UILabel *content1 = [[UILabel alloc]initWithFrame:cFrame];
    //content1.text = @"3000-5000/月";
    content1.text = [NSString stringWithFormat:@"%d-%d/月",self.myCompanyInfo.salaryMin,self.myCompanyInfo.salaryMax];
    content1.textColor = [UIColor orangeColor];
    content1.font = [UIFont systemFontOfSize:self.fontSize + 3];
    [containView addSubview:content1];
    
    //学历要求Label
    cFrame = item1.frame;
    cFrame.origin.y += 25;
    cFrame.size.height = 22;
    UILabel *item2 = [[UILabel alloc]initWithFrame:cFrame];
    item2.text = @"要求:";
    item2.textColor = [UIColor grayColor];
    item2.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:item2];
    
    cFrame.origin.x += 50;
    cFrame.size.width = SCREEN_WIDTH - 30 - 50;
    UILabel *content2 = [[UILabel alloc]initWithFrame:cFrame];
    content2.text = self.myCompanyInfo.educationRequirement;
    content2.textColor = [UIColor grayColor];
    content2.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:content2];
    
    //公司福利Label
    cFrame = item2.frame;
    cFrame.origin.y += 22;
    UILabel *item3 = [[UILabel alloc]initWithFrame:cFrame];
    item3.text = @"福利:";
    item3.textColor = [UIColor grayColor];
    item3.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:item3];
    
    cFrame.origin.x += 50;
    cFrame.size.width = SCREEN_WIDTH - 30 - 50;
    UILabel *content3 = [[UILabel alloc]initWithFrame:cFrame];
    content3.text = self.myCompanyInfo.fringeBenefits;
    content3.textColor = [UIColor grayColor];
    content3.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:content3];
    
    //联系电话Label
    cFrame = item3.frame;
    cFrame.origin.y += 22;
    cFrame.size.width = 70;
    UILabel *item4 = [[UILabel alloc]initWithFrame:cFrame];
    item4.text = @"联系电话:";
    item4.textColor = [UIColor grayColor];
    item4.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:item4];
    
    cFrame.origin.x += 70;
    cFrame.size.width = SCREEN_WIDTH - 30 - 70;
    UILabel *content4 = [[UILabel alloc]initWithFrame:cFrame];
    content4.text = self.myCompanyInfo.tphone;
    content4.textColor = [UIColor redColor];
    content4.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:content4];
    
    //工作地点Label
    cFrame = item4.frame;
    cFrame.origin.y += 22;
    UILabel *item5 = [[UILabel alloc]initWithFrame:cFrame];
    item5.text = @"工作地点:";
    item5.textColor = [UIColor grayColor];
    item5.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:item5];
    
    cFrame.origin.x += 70;
    cFrame.size.width = SCREEN_WIDTH - 30 - 70;
    UILabel *content5 = [[UILabel alloc]initWithFrame:cFrame];
    content5.text = self.myCompanyInfo.workingPlace;
    content5.textColor = [UIColor grayColor];
    content5.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:content5];
    
    //招聘开始时间Label
    cFrame = item5.frame;
    cFrame.origin.y += 22;
    cFrame.size.width = 90;
    UILabel *item6 = [[UILabel alloc]initWithFrame:cFrame];
    item6.text = @"招聘开始时间:";
    item6.textColor = [UIColor grayColor];
    item6.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:item6];
    
    cFrame.origin.x += 95;
    cFrame.size.width = SCREEN_WIDTH - 30 - 95;
    UILabel *content6 = [[UILabel alloc]initWithFrame:cFrame];
    //content6.text = @"2015-10-30 17:27";
    content6.text = [self getTimeWithDate:self.myCompanyInfo.recruitmentTimeStart];
    content6.textColor = [UIColor grayColor];
    content6.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:content6];
    
    //招聘结束时间Label
    cFrame = item6.frame;
    cFrame.origin.y += 22;
    UILabel *item7 = [[UILabel alloc]initWithFrame:cFrame];
    item7.text = @"招聘结束时间:";
    item7.textColor = [UIColor grayColor];
    item7.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:item7];
    
    cFrame.origin.x += 95;
    cFrame.size.width = SCREEN_WIDTH - 30 - 95;
    UILabel *content7 = [[UILabel alloc]initWithFrame:cFrame];
    //content7.text = @"2015-11-02 16:13";
    content7.text = [self getTimeWithDate:self.myCompanyInfo.recruitmentTimeEnd];
    content7.textColor = [UIColor grayColor];
    content7.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:content7];
    
    //分隔线
    cFrame = item7.frame;
    cFrame.origin.y += 25;
    cFrame.size.height = 1;
    cFrame.size.width = lineView.frame.size.width;
    UIView *lineView2 = [[UIView alloc]initWithFrame:cFrame];
    lineView2.backgroundColor = [Tools getColor:@"ebebeb"];
    [containView addSubview:lineView2];
    
    //职位描述WebView
    bFrame.origin.y += 265;
    bFrame.size.height = SCREEN_HEIGHT - 64 - 285;
    UIWebView *webView = [[UIWebView alloc]initWithFrame:bFrame];
    
    NSString *myContent = [self.myCompanyInfo.content stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
    myContent = [NSString stringWithFormat:@"<html><body>%@</body></html>",myContent];
    
    [webView loadHTMLString:myContent baseURL:nil];
    [self.view addSubview:webView];
    UIScrollView *scollview=(UIScrollView *)[[webView subviews]objectAtIndex:0];
    scollview.bounces=NO;
    
}

#pragma mark 日期格式转换
-(NSString *)getTimeWithDate:(NSString *)timeStr{
    
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc]init];
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    [inputFormatter setLocale:locale];
    [inputFormatter setDateFormat:@"MMM dd, yyyy hh:mm:ss aa"];
    NSDate *inputDate = [inputFormatter dateFromString:timeStr];
    //创建日期模板对象,设置模板的显示格式
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
    outputFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
    NSString *outputDate = [outputFormatter stringFromDate:inputDate];     //自动转换成当地时间
    return outputDate;
}


-(void)fetchInfoWithProduct_id:(NSString *)product_id{
    
    //发送请求获取公司产品信息
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"id"] = product_id;
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/company/showInfo.app" parameters:parameters success:^(id responseData) {
        
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        //NSLog(@"result:%@",result);
        
        //去除前面字符串
        if ([result rangeOfString:@"true"].location != NSNotFound) {
            
            [self trimInfo:result WinthNumber:7];
            
        }else if ([result rangeOfString:@"false"].location != NSNotFound) {
            
            [self trimInfo:result WinthNumber:8];
            
        }else{
            
            [Tools alertWithTitle:@"温馨提示" andMessage:@"系统繁忙,请稍后再试"];
            
        }
        
        
    } failure:^(NSError *error) {
        
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
        
    }];
    
}

#pragma mark 去除前面无关紧要的字符
-(void)trimInfo:(NSString *)product WinthNumber:(int)number{
    
    NSMutableString *mstr = [NSMutableString stringWithString:product];
    NSRange range = NSMakeRange(0, number);
    [mstr deleteCharactersInRange:range];
    //json转对象
    //NSLog(@"company:%@",mstr);
    NSData *valueData = [mstr dataUsingEncoding:NSUTF8StringEncoding];
    self.myCompanyInfo = [CGInfoCompany objectWithJSONData:valueData];
    [self layoutUI];
}



@end
